package gob.inei.enaho.model.service;

import gob.inei.enaho.model.domain.ubicacion.Odei;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.android.sqlite.CommandSQLite;
import util.android.sqlite.SQLiteUtil;
import util.patron.command.Command1;
import android.database.Cursor;


public class OdeiService 
extends AbstractService
{
	@Override
	protected String getNombreTabla() {
		return "Odei";
	}
	
	public List<Odei> findAll() throws SQLException {
		final List<Odei> lista = new ArrayList<Odei>();
		String sql = "select * from Odei";
		SQLiteUtil.executeQuery(this.getOpenHelper(),sql,new Command1<Cursor>() {			
			@Override
			public void execute(Cursor obj) throws Exception {
				Odei od = new Odei();
				setearOdei(od,obj);
				lista.add(od);
			}
		});
		return lista;
	}
	
	private void setearOdei(Odei od, Cursor c) {
		od.setCodigo(SQLiteUtil.getString(c, "CodOdei"));
		od.setNombre(SQLiteUtil.getString(c, "NomOdei"));
	}

	public Odei findById(String codigo) throws SQLException {
		String sql = "select * from Odei where CodOdei='"+codigo+"'";		
		return (Odei) SQLiteUtil.executeQueryUnaVez(this.getOpenHelper(),sql,new CommandSQLite() {			
			@Override
			public Object execute(Cursor cr) throws SQLException {
				Odei od = new Odei();
				setearOdei(od,cr);
				return od;
			}
		});
	}
	
}
